$(document).ready(function(){
    $("input[type=text],input[type=password]").keypress(function(event){
        if(event.keyCode == 13){
            $("#btnSignIn").click();
        }
    });
});
    
    function validate(){
      email = $("#email").val();
      password = $("#password").val();
      
      if(email == ''){
        $("#error").text("E-mail is required.");
        return false;
      }
      
      if(password == ''){
        $("#error").text("Password is required.");
        return false;
      }
      
      return true;
    }
    
function sign_in(){
  if(!validate()){ return false; }
  
  var form = $("#frmSignIn");
  var url = $("#action").val();
  
$.ajax({
      type: "POST",
      url: url,
      dataType: "json",
      data: form.serialize(),
      success: function(data, textStatus, xhr) {
        if(data.status == "OK"){
            var next_url = $("#next_url").val();
            if(next_url != ""){
              window.location.href = next_url;
            }else{
              window.location.href = "/";
            }
        }else{
           if(data.message != null){
             $("#error").text(data.message);
           }
        }
      }
    });
  
  return true;
}
